package com.study.servlet;

import com.study.bean.Stu;
import com.study.service.StuService;
import com.study.util.DataUtils;
import com.study.util.PageTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@MultipartConfig
@WebServlet("/stuServlet")
public class StuServlet extends BaseServlet{

    private  StuService ss = new StuService();
    public void updateStudentDo(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        // 获取修改的信息 等同于 添加
        String id = request.getParameter("sid");
        String sname = request.getParameter("sname");
        String gender = request.getParameter("gender");
        String hobby = request.getParameter("hobby");
        String sbirth = request.getParameter("sbirth");
        Part file = request.getPart("file");
        String fileName = file.getSubmittedFileName();
//        // 注意: 在处理头像修改的时候,需要判断是否有上传的头像,有覆盖原头像地址,没有使用原头像地址
        System.out.println("fileName = " + file);
        if(fileName.equals("")){
            String file1 = (String) request.getSession().getAttribute("ph");
            Stu stu = new Stu(sname,gender,DataUtils.stringToDate(sbirth,"yyyy-MM-dd"),hobby,file1);
            stu.setId(Integer.parseInt(id));
            System.out.println(stu);
            int i = ss.updateStu(stu);
            System.out.println("i = " + i);
            request.getRequestDispatcher("stuServlet?mark=getPageInfo");
            if (i > 0) {
                System.out.println("---------");
                response.sendRedirect("stuServlet?mark=getPageInfo");
            }
        }else{

            String last = fileName.substring(fileName.lastIndexOf("."));
            String name =UUID.randomUUID().toString()+last;//照片的最终名字
            String path = "E:\\pic";
            //将照片写入指定磁盘
            file.write(path+ File.separator+name);
            response.sendRedirect("stuServlet?mark=getPageInfo");
        }
        // 跳转到主页面.

    }


    public void queryById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");
        if(id!=null&&id.trim()!=""){
            Stu stu = ss.queryById(id);
            System.out.println(stu);
            String photo = stu.getPhoto();
            request.getSession().setAttribute("ph", photo);
            request.getSession().setAttribute("sid1", id);
            request.getSession().setAttribute("stu",stu);
            request.getRequestDispatcher("updateToJsp.jsp").forward(request,response);
        }


    }
    public void batchDel(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");
        if(id!=null&&id.trim()!=""){
            String[] sid = id.split(",");
            for (String id1 : sid) {
                System.out.println(id1);
                ss.delStu(Integer.parseInt(id1));
            }
        }
        response.sendRedirect("stuServlet?mark=getPageInfo");

    }
    public void updateToJsp(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // 获取要修改的学生编号
        String id = request.getParameter("id");
        // 根据学生编号从数据库获取学生信息
        Stu stu = ss.queryById(id);
        // 将学生信息存放作用域
        request.setAttribute("stu",stu);
        // 跳转到修改页面
        request.getRequestDispatcher("updateStu.jsp").forward(request,response);
    }


    public void getPageInfo(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
       String sname = request.getParameter("sname");
       String gender = request.getParameter("gender");
       String currentPage = request.getParameter("currentPage");
       Stu student = new Stu(sname, gender);

       PageTools<Stu> pageInfo = ss.getPageInfo(student, currentPage);
       request.setAttribute("stu",student);
       request.setAttribute("page",pageInfo);
       request.getRequestDispatcher("queryStu.jsp").forward(request,response);

   }
   public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
       String sname = req.getParameter("sname");
       String gender = req.getParameter("gender");
       String hobby = req.getParameter("hobby");
       String sbirth = req.getParameter("sbirth");

        //获取照片的名字
       Part file = req.getPart("file");
       String fileName = file.getSubmittedFileName();
       System.out.println("fileName = " + fileName);
       String last = fileName.substring(fileName.lastIndexOf("."));
       String name =UUID.randomUUID().toString()+last;//照片的最终名字
       String path = "E:\\pic";
        //将照片写入指定磁盘
       file.write(path+ File.separator+name);

       Stu stu = new Stu(sname, gender, DataUtils.stringToDate(sbirth, "yyyy-MM-dd"), hobby, name);
       StuService ss = new StuService();
       boolean b = ss.addStu(stu);
       if(b){
           resp.sendRedirect("stuServlet?mark=getPageInfo");
       }else{
           req.getRequestDispatcher("addStu.html").forward(req,resp);
       }
   }

}
